package com.derbysoft.synxisadapter.dswitch3.translator;

import com.derbysoft.dswitch.remote.hotel.dto.HotelCancelRequest;
import com.derbysoft.synxisadapter.commons.dto.synxis.*;
import com.derbysoft.synxisadapter.commons.exception.ReservationNumberNotFoundException;
import com.derbysoft.synxisadapter.commons.model.Reservation;
import com.derbysoft.synxisadapter.commons.model.ReservationItem;
import com.derbysoft.synxisadapter.commons.repository.ReservationRepository;
import com.derbysoft.synxisadapter.commons.utils.Constant;
import com.derbysoft.synxisadapter.dswitch2.translator.Translator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * User: jason
 * Date: 2012-11-29
 */
@Component("ds3CancelRQTranslator")
public class HotelCancelRQTranslator extends AbstractTranslator
        implements Translator<HotelCancelRequest, List<OTACancelRQ>, Void> {

    @Autowired
    private ReservationRepository reservationRepository;

    @Override
    public List<OTACancelRQ> translate(HotelCancelRequest request, Void aVoid) {
        List<OTACancelRQ> otaCancelRQs = new ArrayList<OTACancelRQ>();
        Reservation reservation = reservationRepository.findByErspCode(request.getHotelCancelRQ().getErsp());
        if (reservation == null) {
            throw new ReservationNumberNotFoundException(request.getHotelCancelRQ().getErsp());
        }
        List<ReservationItem> reservationItems = reservationRepository.loadReservationItems(reservation);
        for (ReservationItem reservationItem : reservationItems) {
            OTACancelRQ otaCancelRQ = createOTACancelRQ();
            otaCancelRQ.getUniqueID().add(createUniqueID(reservationItem.getConfirmNumber()));
            otaCancelRQ.setPrimaryLangID(Constant.PRIMARY_LANG_ID);
            otaCancelRQ.setPOS(translatePOS(request.getHeader().getSource()));
            otaCancelRQ.setVerification(translateVerification(reservation.getHotelId()));
            otaCancelRQs.add(otaCancelRQ);
        }
        return otaCancelRQs;
    }

    private UniqueID createUniqueID(String confirmNumber) {
        UniqueID uniqueID = createUniqueID();
        uniqueID.setType("14");
        uniqueID.setID(confirmNumber);
        uniqueID.setIDContext(Constant.CONTEXT_CRS_CONFIRM_NUMBER);
        return uniqueID;
    }

    private Verification translateVerification(String hotelId) {
        Verification verification = createVerification();
        verification.setTPAExtensions(translateTPAExtensions(hotelId));
        return verification;
    }

    private TPAExtensions translateTPAExtensions(String hotelId) {
        TPAExtensions extensions = createTPAExtensions();
        extensions.setBasicPropertyInfo(translateBasicPropertyInfoType(hotelId));
        return extensions;
    }

    private BasicPropertyInfoType translateBasicPropertyInfoType(String hotelId) {
        BasicPropertyInfoType basicPropertyInfoType = createBasicPropertyInfoType();
        basicPropertyInfoType.setHotelCode(hotelId);
        return basicPropertyInfoType;
    }
}
